package com.xsp.order.service.impl;

import com.xsp.order.client.EduClient;
import com.xsp.order.client.UcenterClient;
import com.xsp.order.entity.Order;
import com.xsp.order.mapper.OrderMapper;
import com.xsp.order.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xsp.order.utils.OrderNoUtil;
import com.xsp.ordervo.CourseWebVoOrder;
import com.xsp.ordervo.UcenterMemberOrder;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author xsp
 * @since 2022-03-02
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    EduClient eduClient;
    @Autowired
    UcenterClient ucenterClient;

    //生成订单
    //memberIdByJwtToken实际上就是memberId(通过token解析得到的)
    @Override
    public String saveOrder(String courseId, String memberIdByJwtToken) {
        //远程调用 根据用户id获取用户信息
        UcenterMemberOrder ucenterMemberOrder = ucenterClient.getInfo(memberIdByJwtToken);
        //远程调用 根据课程id获取课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

        //创建订单
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());//订单号
        order.setCourseId(courseId);//课程id
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberIdByJwtToken);
        order.setMobile(ucenterMemberOrder.getMobile());//用户手机
        order.setNickname(ucenterMemberOrder.getNickname());//用户昵称
        order.setStatus(0);//支付状态,0是未支付
        order.setPayType(1);//支付类型,1是微信

        baseMapper.insert(order);

        //必须从对象中拿到订单号,如果调用工具类,得到的是新的随机订单号
        return order.getOrderNo();
    }
}
